package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Environment;
import android.util.Log;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class AudioTrackManager {
    private static final String TAG = AudioTrack.class.getSimpleName();

    public static String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audio.pcm";

    private static AudioTrackManager instance;

    private int mSampleRate = 44100;

    private int channelCount = 2;

    private AudioTrack mAudioTrack;

    private int channelConfig = AudioFormat.CHANNEL_IN_STEREO;

    private int bufferSize;

    private int audioFormatEncode = AudioFormat.ENCODING_PCM_16BIT;

    private AudioManager mAudioManager;

    private Context mContext;

    private AudioFormat mAudioFormat;

    private FileInputStream fileInputStream;

    private boolean startPlay = false;

    public AudioTrackManager() {

    }

    public static AudioTrackManager getInstance() {
        if (instance == null) {
            instance = new AudioTrackManager();
        }
        return instance;
    }

    public void setContext(Context context) {
        this.mContext = context;
        init();
    }

    @SuppressLint("NewApi")
    private void init() {
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        bufferSize = AudioTrack.getMinBufferSize(mSampleRate, channelConfig, audioFormatEncode);
        int sessionId = mAudioManager.generateAudioSessionId();
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
        AudioFormat audioFormat = new AudioFormat.Builder().setSampleRate(mSampleRate)
                .setEncoding(audioFormatEncode)
                .setChannelMask(channelConfig)
                .build();
        mAudioTrack = new AudioTrack(audioAttributes, audioFormat, bufferSize * 2, AudioTrack.MODE_STREAM, sessionId);
        try {
            fileInputStream = new FileInputStream(PATH);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "init: " + e);
        }
    }

    public void startThread() {
        startPlay = true;
        new PlayThread().start();
    }

    public void stopThread() {
        startPlay = false;
        mAudioTrack.stop();
        mAudioTrack.release();
    }

    class PlayThread extends Thread {
        @Override
        public void run() {
            super.run();
            byte[] buffer = new byte[bufferSize];
            mAudioTrack.play();
            try {
                while (fileInputStream.read(buffer) > 0) {
                    mAudioTrack.write(buffer, 0, buffer.length);
                }

            } catch (IOException e) {
                Log.e(TAG, "IOException " + e);
            }

        }
    }
}
